package com.share.function.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.share.function.bean.UserBean;
import com.share.function.dao.IpDao;
import com.share.function.dao.LoginDao;

public class LoginService {

	private LoginDao loginDao;
	private IpDao ipDao;

	/**
	 * 判断登录是否成功
	 * 
	 * @param loginUser
	 * @return
	 */
	public Map<String, Object> getMap(UserBean loginUser, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		if (null != request.getAttribute("sLoginName") && null != request.getAttribute("sPassword")) {
			loginUser.setsLoginName((String) request.getAttribute("sLoginName"));
			loginUser.setsPassword((String) request.getAttribute("sPassword"));
		}
		UserBean user = loginDao.getUser(loginUser);
		// 默认登录失败
		map.put("status", -1);
		map.put("nextUrl", "/jsp/login/login.jsp");
		// 判断登录是否成功
		if (loginUser.getsLoginName().isEmpty() || loginUser.getsPassword().isEmpty()) {
			return map;
		} else if (null != user && user.getsPassword().equals(loginUser.getsPassword())) {
			map.put("status", 1);
			map.put("nextUrl", "/jsp/login/main.jsp");
			user = loginDao.getPowerList(loginUser);
			user.setsIp(getIp(request));
			ipDao.inertIp(user);
			map.put("loginUser", user);
			System.out.println("LoginService.getMap.user--" + user);
		}
		return map;
	}

	/**
	 * 设置自动登陆
	 * 
	 * @param request
	 * @param map
	 * @param response
	 */
	public void setAutoLogin(HttpServletRequest request, Map<String, Object> map, HttpServletResponse response) {
		System.out.println("LoginService.setAutoLogin.status==1--" + (1 == (int) map.get("status")));
		if (1 == (int) map.get("status")) {
			// System.out.println("LoginService.setAutoLogin.autoLogin.equsls(\"1\")--"
			// + request.getParameter("autoLogin").equals("1"));
			if (null != request.getParameter("autoLogin") && request.getParameter("autoLogin").equals("1")) {
				int saveTime = 600;
				UserBean loginUser = (UserBean) map.get("loginUser");
				Cookie loginUserCookie = new Cookie("loginUser",
						loginUser.getsLoginName() + "==" + loginUser.getsPassword());
				loginUserCookie.setMaxAge(saveTime);
				loginUserCookie.setPath("/");
				response.addCookie(loginUserCookie);
				System.out.println("LoginService.setAutoLogin.Cookie--" + loginUserCookie.getValue());
			}
		}
	}

	/**
	 * 根据登录用户得到历史登录信息列表
	 * 
	 * @param loginUser
	 * @return
	 */
	public Map<String, Object> getLoginMsgMap(UserBean loginUser) {
		Map<String, Object> map = new HashMap<>();
		loginUser.setCount(ipDao.getCountRow(loginUser).getCount());
		System.out.println("loginUser.getBeginRow--" + loginUser.getBeginRow());
		List<Map<String, Object>> list = ipDao.getIpList(loginUser);
		System.out.println("LoginService.getLoginMsgMap.ipList--" + list);
		System.out.println("LoginService.getLoginMsgMap.count--" + ipDao.getCountRow(loginUser).getCount());
		map.put("page", loginUser.getPage());
		map.put("pages", loginUser.getPages());
		map.put("order", loginUser.getOrder());
		map.put("desc", loginUser.getDesc());
		map.put("ipList", list);
		map.put("nextUrl", "/jsp/login/loginMsg.jsp");
		return map;
	}

	/**
	 * 登录成功后得到当前登录IP地址
	 * 
	 * @param request
	 */
	private String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			ip = "本地";
		}
		return ip;
	}

	public LoginDao getLoginDao() {
		return loginDao;
	}

	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

	public IpDao getIpDao() {
		return ipDao;
	}

	public void setIpDao(IpDao ipDao) {
		this.ipDao = ipDao;
	}

}
